package com.lipuwater.agent.ui.popwindown;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.adapter.BaseViewHolder;
import com.lipuwater.agent.ui.adapter.PublicListAdapter;

import java.util.List;

/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2017/4/12.
 * Email:924686754@qq.com
 */
public class SearchFilterPop extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {
    private Activity mActivity;
    private TextView mTextView;
    private View mBackView;
    private List<String> mData;
    private View mPopView;

    private RecyclerView mRecyclerView;
    private PublicListAdapter adapter;
    private int mChoosePosition = 0;
    private ChooseCallback mChooseCallback;

    public SearchFilterPop(final Activity activity, TextView textView, View backView, List<String> data, ChooseCallback chooseCallback) {
        super(activity);
        this.mActivity = activity;
        this.mTextView = textView;
        this.mData = data;
        this.mBackView = backView;
        this.mChooseCallback = chooseCallback;
        this.mBackView.setOnClickListener(this);
        mPopView = mActivity.getLayoutInflater().inflate(R.layout.pop_search_filter, null);
        setContentView(mPopView);
        setBackgroundDrawable(new ColorDrawable(0));
        setAnimationStyle(R.style.PopAnim);
        update();
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setTouchable(true); // 设置popupwindow可点击
        setOnDismissListener(this);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        mRecyclerView = (RecyclerView) mPopView.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new PublicListAdapter(mActivity, R.layout.item_text, mData);
        adapter.setViewHolder(new PublicListAdapter.ViewHolder() {
            @Override
            public void onBindViewHolder(BaseViewHolder holder, final int position) {
                TextView text = (TextView) holder.getView(R.id.text);
                text.setText(mData.get(position));
                if (mChoosePosition == position) {
                    Drawable drawable = ContextCompat.getDrawable(mActivity, R.drawable.icon_yes);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                            drawable.getMinimumHeight());
                    text.setCompoundDrawables(null, null, drawable, null);
                } else {
                    text.setCompoundDrawables(null, null, null, null);
                }
                text.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mChoosePosition = position;
                        adapter.notifyDataSetChanged();
                        if (mChooseCallback != null) {
                            mChooseCallback.results(position, mData.get(position));
                        }
                        dismiss();
                    }
                });
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    public void show() {
        //关闭软键盘
        final View v = mActivity.getWindow().peekDecorView();
        if (v != null && v.getWindowToken() != null) {
            InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(mActivity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        if (this.isShowing()) {
            setDrawable(false);
            dismiss();
            mBackView.setVisibility(View.GONE);
        } else {
            setDrawable(true);
            mBackView.setVisibility(View.VISIBLE);
            // 设置popupwindow的位置
            showAsDropDown(mTextView, 0, 0);
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    /**
     * 设置textView图标
     */
    public void setDrawable(boolean top) {
        Drawable drawable = null;
        if (top) {
            drawable = ContextCompat.getDrawable(mActivity, R.drawable.icon_top);
        } else {
            drawable = ContextCompat.getDrawable(mActivity, R.drawable.icon_bottom);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        mTextView.setCompoundDrawables(null, null, drawable, null);
    }

    @Override
    public void onDismiss() {
        setDrawable(false);
        mBackView.setVisibility(View.GONE);
    }

    public interface ChooseCallback {
        void results(int position, String string);
    }
}
